﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace App
{
    public partial class NewTeam : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Common.GetLoggedUser(this.Page) == null)
            {
                Response.Redirect("Login.aspx");
                return;
            }
        }

        protected void ButtonNewTeam_Click(object sender, EventArgs e)
        {
            
            LabelErrorMessage.Visible = true;

            AppLib.Team newTeam = new AppLib.Team();

            newTeam.TeamName = TextBoxTeamName.Text;
            newTeam.Location = TextBoxLocation.Text;
            newTeam.DateOfFoundation = DateTime.Now;
            newTeam.LeaderID = Common.GetLoggedUser(this).UserID;
            newTeam.Active = true;
            newTeam.Description = TextBoxInfo.Text;
            newTeam.ImageURL = TextBoxTeamAvatarURL.Text;

            if (!MidApp.ManageTeam.TeamNameAlreadyTaken(TextBoxTeamName.Text))
            {
                LabelErrorMessage.Visible = false;
                
                MidApp.ManageTeam.CreateNewTeam(newTeam);
                
                MidApp.ManageIsMember.ApproveUserToTeam(Common.GetLoggedUser(this.Page).UserID, MidApp.ManageTeam.GetTeamByTeamName(newTeam.TeamName).TeamID,true,false);

                
                Response.Redirect("Default.aspx");
            }

        }
    }
}